<?php

function scribe_step_1() {
	global $user;
	$user_all = user_load($user->uid);
	$output = '';
	
	$output .= '<div class="items-list">';

	$query = db_select('user_scribe','a')
		->fields('a')
		->condition('a.uid', $user->uid)
		->condition('a.status', 2)
		->orderBy('a.created', 'DESC');
  	$items = $query->execute()->fetchAll();

  	$header = array(
  		array('data' => t('序号')),
    	array('data' => t('书名')),
    	array('data' => t('订阅时间'))
  	);
	
	$row = array();
	
	$i = 1;
	foreach($items as $item){
		$node = node_load($item->nid);
		$row[] = array('data' => 
					array(
						$i,
    					'《'.$node->title.'》',
    					format_date($item->created, 'custom', 'Y-m-d')
    				)
  				);
    	$i++;
	}
	

	$output .= theme('table', 
					array(
						'header' =>$header, 
						'rows' => $row, 
						'attributes' => array('class' => array('selectbooks')),
						'caption' => t('要归还的图书')
					)
				);
	
	$query = db_select('user_scribe','a')
		->fields('a')
		->condition('a.uid', $user->uid)
		->condition('a.status', 1)
		->orderBy('a.created', 'DESC');
  	$items = $query->execute()->fetchAll();

  	$header = array(
  		array('data' => t('序号')),
    	array('data' => t('书名')),
    	array('data' => t('订阅时间')),
  	);
	
	$row = array();
	
	$i = 1;
	foreach($items as $item){
		$node = node_load($item->nid);
		$row[] = array('data' => 
					array(
						$i,
    					'《'.$node->title.'》',
    					format_date($item->created, 'custom', 'Y-m-d')
    				)
  				);
    	$i++;
	}
	

	$output .= theme('table', 
					array(
						'header' =>$header, 
						'rows' => $row, 
						'attributes' => array('class' => array('selectbooks')),
						'caption' => t('借阅的图书')
					)
				);

	$rows = array();

	$roles = implode($user->roles, ',');

	$rows[] = array('data' => 
					array(
						'性名:',
    					$user->name
    				)
  				);

	$rows[] = array('data' => 
					array(
						'用户类型:',
    					$roles
    				)
  				);

	$phone = field_get_items('user', $user_all, 'field_user_phone');
	$rows[] = array('data' => 
					array(
						'电话:',
    					$phone[0]['value']
    				)
  				);	

	$address = field_get_items('user', $user_all, 'field_user_address');
	$rows[] = array('data' => 
					array(
						'地址:',
    					$address[0]['value']
    				)
  				);
	
	$output .= theme('table', 
					array(
						'', 
						'rows' => $rows, 
						'attributes' => array('class' => array('selectbooks')),
						'caption' => t('个人信息')
					)
				);

	$form = drupal_get_form('select_step_form');

	$output .= drupal_render($form);

	$output .= '</div>';
	
	return $output;
}

function select_step_form() {
	global $user;
	$form = array();
	$form['explain'] = array(
		'#title' => t('说明'),
		'#type' => 'textarea',
		'#rows' => 2,
	);		
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('订单提交'),
	);		
	return $form;
}

function select_step_form_submit($form, &$form_state) {

	$form = array();

	global $user;

	$explain = $form_state['values']['explain'];

	$id = db_insert('admin_d')
  	->fields(array('k_uid' => $user->uid, 'created' => REQUEST_TIME, 'book_explain' => $explain))
  	->execute();

  	$query = db_select('user_scribe','a')
		->fields('a')
		->condition('a.uid', $user->uid)
		->condition('a.status', 1);
  	$items = $query->execute()->fetchAll();

  	foreach($items as $value) {
  		db_insert('admin_d_item')
  		->fields(array('did' => $id, 'nid' => $value->nid))
  		->execute();
  	}

  	$query = db_select('user_scribe','a')
		->fields('a')
		->condition('a.uid', $user->uid)
		->condition('a.status', 2);
  	$items = $query->execute()->fetchAll();

  	foreach($items as $value) {
  		db_insert('admin_d_item')
  		->fields(array('did' => $id, 'nid' => $value->nid, 'status' => 0))
  		->execute();
  	}

  	db_update('user_info')
  	->fields(array('status' => 2))
  	->condition('uid', $user->uid)
  	->execute();

  	$data = array('query' => array('id' => $id));

  	$form_state['redirect'] = array('step_2', $data);
	return $form;
}

function scribe_step_2() {
	$output = '<p>您的订单已生效，我们将尽快为你配书送货，感谢您的支持！<p>';

	$id = $_GET['id'];

	$output .= '订单号：' . str_pad($id, 5,"0",STR_PAD_LEFT);

	return $output;

}

function bill_list() {

	$output = '';

	$query = db_select('admin_d','a')
		->fields('a')
		->orderBy('a.created', 'DESC');
  	$items = $query->execute()->fetchAll();

  	foreach ($items as $key => $value) {
  		$user_kh = user_load($value->kid); 
  		$rows[] = array(
  			'data' => array(
  					str_pad($value->id, 5,"0",STR_PAD_LEFT),
  					$user_kh->name,
  					$user_kh->address,
  					$user_kh -> phone
  				)
  			);
  	}

  	$header = array(
  		array('data' => '订单号'),
  		array('data' => '客户姓名'),
  		array('data' => '地址'),
  		array('data' => '电话')
  		);

  	$output .= theme('table', array($header, $rows));

	return $output;
}